Create a custom Collector

public static <T> Collector<T, ?, T> toSingleton() {
    return Collectors.collectingAndThen(
            Collectors.toList(),
            list -> {
                if (list.size() != 1) {
                    throw new IllegalStateException();
                }
                return list.get(0);
            }
    );
}

We use Collectors.collectingAndThen to construct our desired Collector by

  1. Collecting our objects in a List with the Collectors.toList() collector.
  2. Applying an extra finisher at the end, that returns the single element — or throws an IllegalStateException if list.size != 1.

Used as:

User resultUser = users.stream()
        .filter(user -> user.getId() > 0)
        .collect(toSingleton());

You can then customize this Collector as much as you want, for example give the exception as argument in the constructor, tweak it to allow two values, and more.

An alternative — arguably less elegant — solution:

You can use a 'workaround' that involves peek() and an AtomicInteger, but really you shouldn't be using that.

What you could do istead is just collecting it in a List, like this:

LinkedList<User> users = new LinkedList<>();
users.add(new User(1, "User1"));
users.add(new User(2, "User2"));
users.add(new User(3, "User3"));
List<User> resultUserList = users.stream()
        .filter(user -> user.getId() == 1)
        .collect(Collectors.toList());
if (resultUserList.size() != 1) {
    throw new IllegalStateException();
}
User resultUser = resultUserList.get(0);

Stream (Java Platform SE 8 )

https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html

Streams are created with an initial choice of sequential or parallel execution. (For example, Collection.stream() creates a sequential stream, and Collection.

Java 8 Stream | 菜鸟教程

https://www.runoob.com/java/java8-streams.html

Stream API可以极大提高Java程序员的生产力,让程序员写出高效率、干净、简洁 的代码。 这种风格将要处理的元素集合看作一种流, 流在管道中传输, 并且可以 在 ...

Package java.util.stream

https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html

No storage. A stream is not a data structure that stores elements; instead, it conveys elements from a source such as a data structure, an array, a generator ...

The Java 8 Stream API Tutorial | Baeldung

https://www.baeldung.com/java-8-streams

May 14, 2021 ... In this comprehensive tutorial, we'll go through the practical uses of Java 8 Streams from creation to parallel execution. To understand this ...

Stream In Java - GeeksforGeeks

https://www.geeksforgeeks.org/stream-in-java/

Oct 9, 2019 ... Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods ...

A Guide to Java Streams in Java 8: In-Depth Tutorial With Examples ...

https://stackify.com/streams-guide-java-8/

Mar 18, 2020 ... Java Stream Specializations. From what we discussed so far, Stream is a stream of object references. However, there are also the IntStream, ...

Java 8 Stream Tutorial

https://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/

Jul 31, 2014 ... Learn Java 8 streams by example: functional programming with filter, map, flatMap, reduce, collect, lambdas, sequential and parallel streams ...

Java Stream API

http://tutorials.jenkov.com/java-functional-programming/streams.html

Apr 28, 2019 ... The Java Stream API refers to the functional stream processing API added to Java in Java 8. The Java Stream API provides a functional ...

Filter Java Stream to 1 and only 1 element - Stack Overflow

https://stackoverflow.com/questions/22694884/filter-java-stream-to-1-and-only-1-element/50514439

Create a custom Collector. public static <T> Collector<T, ?, T> toSingleton() { return Collectors.collectingAndThen( Collectors.toList(), list -> { if ...